unit roboget;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, OleCtrls, SHDocVw, inifiles, Rijndael;

type
  TForm1 = class(TForm)
    wb: TWebBrowser;
    Button1: TButton;
    Memo1: TMemo;
    OpenDialog1: TOpenDialog;
    wb2: TWebBrowser;
    procedure Button1Click(Sender: TObject);
    procedure wbDocumentComplete(Sender: TObject; const pDisp: IDispatch;
      var URL: OleVariant);
    procedure FormCreate(Sender: TObject);
  private
    procedure doprocess;
    procedure writeval(title, url, field, value: string);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses fileutils;
{$R *.dfm}

var
  crypt_rijndael: TDCP_Rijndael;
const
  aPASSPHRASE = 'my spoon is too big';
  aFILENAME = 'secure.ini';
  aTMPFILENAME = 'tmp.ini';


function decodestring(key, s: string): string;
var i: integer;
begin
  crypt_rijndael.InitStr(key);

  crypt_rijndael.DecryptCFB(s[1], s[1], length(s));

  crypt_rijndael.Reset();
  crypt_rijndael.Burn();

  result := s;
end;

function encodestring(key, s: string): string;
var i: integer;
begin
  crypt_rijndael.InitStr(key);

  crypt_rijndael.EncryptCFB(s[1], s[1], length(s));

  crypt_rijndael.Reset();
  crypt_rijndael.Burn();

  result := s;
end;

function LocalPath: string;
begin
  Result := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if (opendialog1.Execute) then
    wb.Navigate(opendialog1.FileName);
end;

procedure TForm1.wbDocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  CurWebrowser: IWebBrowser;
  TopWebBrowser: IWebBrowser;
  Document: OleVariant;
  WindowName: string;
begin
  CurWebrowser := pDisp as IWebBrowser;
  TopWebBrowser := (Sender as TWebBrowser).DefaultInterface;

  if CurWebrowser = TopWebBrowser then
  begin
    doprocess;
  end;
end;

procedure TForm1.doprocess;
var
  i, j, k: integer;
  tables, table, trs, tr, tds, td: variant;
  title, url, field, value: string;
  count: integer;
begin
  tables := wb.OleObject.Document.getElementsByTagName('TD');
  count := 0;

  for i := 0 to tables.length - 1 do
  begin
    table := tables.Item(i);
    if (table.getElementsByTagName('TABLE').length = 1) then
    begin

      trs := table.getElementsByTagName('TR');
      title := ''; url := ''; field := ''; value := '';

      for j := 0 to trs.length - 1 do
      begin
        inc(count);
        tr := trs.item(j);
        tds := tr.getElementsByTagName('TD');
        if (tds.length <= 3) then
        begin
          for k := 0 to tds.length - 1 do
          begin
            td := tds.item(k);

            if (td.className = 'caption') then
              title := td.innerText
            else if (td.className = 'subcaption') then
              url := td.innerText
            else if ((title <> '') and (url <> '') and (td.className = 'field')) then
              field := td.innerText
            else if ((field <> '') and (td.className = 'wordbreakfield')) then
            begin
              value := td.innerText;
              writeval(ExtractFileName(title), url, field, value);
              memo1.lines.add(title + ', ' + url + ', ' + field + ', ' + value);
              memo1.lines.add('---');
              field := '';
            end;
          end;
        end;
      end;
    end;
  end;

  writefile(LocalPath + aFILENAME, encodestring(aPASSPHRASE, readfile(LocalPath + aTMPFILENAME)));
  deletefile(LocalPath + aTMPFILENAME);

  showmessage(inttostr(count) + ' entries were successfully parsed / imported!');
  showmessage('Please close the "Chrome Form Filler Service" and start it again to reflect these new imports (or reboot your computer)');
  application.Terminate;
end;

procedure TForm1.writeval(title, url, field, value: string);
var
  newIni: TIniFile;
begin
  newIni := TIniFile.Create(LocalPath + aTMPFILENAME);
  try
    with newIni do
    begin
      WriteString(title, 'url', url);
      WriteString(title, field, value);
      UpdateFile;
    end;
  finally
    newIni.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  wb2.Navigate(localpath + 'import.mht');
end;

initialization
  crypt_rijndael := TDCP_Rijndael.Create(nil);
end.

